﻿using TodoList.Data;
using TodoList.MinimalApi.DTOs;
using TodoList.MinimalApi.Mapper;

namespace TodoList.MinimalApi.Endpoints.TodoList;

public class Update : IEndpoint
{
	public void MapEndpoint(IEndpointRouteBuilder endpoints)
	{
		endpoints.MapPut("/api/todoList/{id:int}", async (ApplicationDbContext db,int id,UpdateTodoItemRequest request) =>
			{
				var todoItem = await db.TodoItems.FindAsync(id);
				if (todoItem == null)
				{
					return Results.NotFound("Todo item not found");
				}
				todoItem.UpdateEntity(request);
				db.TodoItems.Update(todoItem);
				await db.SaveChangesAsync();
				return Results.Ok("Todo item updated successfully");
			})
			.WithName("UpdateTodoItem")
			.WithTags(Tags.TodoList);
	}
}
